<?php
defined ( 'IN_SYS' ) or die ( 'Restricted Access' );
require_once SYS_LIB_KERNEL.'sys.view.php';
class SysAction extends SysView {
	protected $myClass;
	protected $lang = array();
	public $spec = '';
	public $action = NULL;
	public $tpl = NULL;
	public $page = NULL;
	protected $layout = NULL;
	
    function __construct() {
    	
    }
    
    function __begin($page,$portal) {
    	$page = substr($page, 0, -6);
    	$this->page = $page;
    	$this->action = $portal.'/'.$page;
    }
    
    /**
     * set template
     * @param string $file tpl name
     * @param string $portal tpl path
     */
    function tpl($file='', $portal=NULL) {
    	if (is_null($portal)) $portal = Page::get('SYS_PORTAL');
    	$tpl = SYS_APPS_VIEWER . 'templates' . DS . $portal . DS . $file . '.tpl';
    	
    	$this->setTpl(get_class($this),$tpl);
    	$this->getLangsPrivate($portal, $file);
    	
    	$this->assign('PATH_SKINS', SYS_SKINS);
    	
    	Page::set('SYS_TEMPLATE', $portal . DS . $file . '.tpl');
    	$this->tpl = $portal . DS . $file . '.tpl';
    }
    
    /**
     * return html
     */
    function output() {
    	return $this->getOutput(get_class($this));
    }
    
    /**
     * create block
     */
    function block($blockName='') {
    	return $this->setBlock(get_class($this), $blockName);
    }
    
    /**
     * push data in block
     */
    function addBlock($blockName='') {
    	return $this->add_block($blockName);
    }
    
    /**
     * return data of block
     */
    function getBlock($blockName='') {
    	return $this->get_block($blockName);
    }
    
    /**
     * @desc get languages file of template
     */
    function getLangsPrivate($portal, $file) {
    	$this->lang += Page::getLangsPrivate($portal, $file);
    	return $this->lang;
    }
    /**
     * @desc get languages file of template
     */
    function getLangsPublic () {
    	$this->lang = Page::getLangsPublic();
    	return $this->lang;
    }
    
    /**
     * go Referer
     * @param string $param
     * @param string $page
     * @param string $portal
     */
    public function goRererer($param=array(), $page='', $portal='') {
		return Page::goReferer($param, $page, $portal);
    }
    
    function goFinish() {
    	if ($this->getSpec() != '') {
    		Page::setSession('ScreenId', $this->getSpec());
    	}
    	return Page::goUrl(NULL, 'Finish', 'setting');
    }
    
    /**
	 * setSpec
	 * @param $name
	 */
    function setSpec($name='') {
    	$this->spec = $name;
    	Page::setSpec($name);
    }
    function getSpec() {
    	return $this->spec;
    }
    
    /**
	 * setTitle()
	 * @param $title name
	 */
    function setTitle($title='') {
    	if (isset($this->lang[$title])) {
    		Page::setTitle($this->lang[$title]);
    	} else {
    		Page::setTitle(' ');
    	}
    	
    }

	public function setLayout($file=NULL) {
		$path = SYS_APPS_VIEWER . 'layouts' . DS . $file . '.layout';
		if (file_exists($path)) {
			$this->layout = $path;
		} else {
			throw new SysExceptionControl('Do not found layout file: '.$path);
		}
	}
	
	public function hasLayout() {
		return !is_null($this->layout);
	}
	
	public function getLayout() {
		return sys_file_get_contents($this->layout);
	}
}